package com.dhcc.bpm.modules.log.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.log.entity.GatewayLog;
import com.dhcc.bpm.modules.log.service.IGatewayLogService;
import com.dhcc.bpm.modules.log.vo.GatewayLogVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
/**
 * @ClassName GatewayLogController.class
 * @Description  网关访问记录表 控制器
 * @Author xiaFaYu
 * @Date 2020-02-03 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-gateway/gatewaylog")
@Api(value = "网关访问记录表", tags = "网关访问记录表接口")
public class GatewayLogController extends BladeController {

	private IGatewayLogService gatewayLogService;

	/**
	 * @Description 详情
	 * @Author xiafayu
	 * @Date 2020/2/24 14:54
	 * @param gatewayLog
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.log.entity.GatewayLog>
	 **/
	@ApiLog("网关访问记录详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入gatewayLog")
	@Log(title = "网关访问记录详情", businessType = BusinessType.SELECT)
	public R<GatewayLog> detail(GatewayLog gatewayLog) {
		GatewayLog detail = gatewayLogService.getOne(Condition.getQueryWrapper(gatewayLog));
		return R.data(detail);
	}

	/**
	  * @Description 分页 网关访问记录表
	  * @Author xiafayu
	  * @Date 2020/2/24 14:54
	  * @param gatewayLog
      * @param query
	  * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.log.entity.GatewayLog>>
	  **/
	@ApiLog("网关访问记录分页")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入gatewayLog")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "opcApiCode", value = "接口编码", paramType = "query", dataType = "String"),
	})
	@Log(title = "网关访问记录分页", businessType = BusinessType.SELECT)
	public R<IPage<GatewayLog>> list(@ApiIgnore @RequestParam Map<String, Object> gatewayLog , Query query) {
		IPage<GatewayLog> pages = gatewayLogService.page(Condition.getPage(query),Condition.getQueryWrapper(gatewayLog, GatewayLog.class));
		return R.data(pages);
	}

	/**
	 * @Description 分页 网关访问记录表
	 * @Author xiafayu
	 * @Date 2020/2/24 14:55
	 * @param gatewayLog
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.log.entity.GatewayLog>>
	 **/
	@ApiLog("网关访问记录分页")
	@PostMapping("/gatewayLoglist")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入gatewayLog")
	@Log(title = "网关访问记录分页", businessType = BusinessType.SELECT)
	public R<IPage<GatewayLog>> gatewayLoglist(@ApiIgnore @RequestParam Map<String, Object> gatewayLog , Query query) {
		IPage<GatewayLog> pages = gatewayLogService.page(Condition.getPage(query),Condition.getQueryWrapper(gatewayLog, GatewayLog.class));
		return R.data(pages);
	}
	/**
	 * @Description 自定义分页 网关访问记录表
	 * @Author xiafayu
	 * @Date 2020/2/24 14:55
	 * @param gatewayLog
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.log.vo.GatewayLogVO>>
	 **/
	@ApiLog("网关访问记录分页")
	@GetMapping("/listByApi")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入gatewayLog")
	@Log(title = "网关访问记录分页", businessType = BusinessType.SELECT)
	public R<IPage<GatewayLogVO>> listByApi(GatewayLogVO gatewayLog, Query query) {
		IPage<GatewayLogVO> pages = gatewayLogService.selectGatewayLogByApi(Condition.getPage(query), gatewayLog);
		return R.data(pages);
	}
	/**
	 * @Description 自定义分页 网关访问记录表
	 * @Author xiafayu
	 * @Date 2020/2/24 14:55
	 * @param gatewayLog
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.log.vo.GatewayLogVO>>
	 **/
	@ApiLog("网关访问记录分页")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入gatewayLog")
	@Log(title = "网关访问记录分页", businessType = BusinessType.SELECT)
	public R<IPage<GatewayLogVO>> page(GatewayLogVO gatewayLog, Query query) {
		IPage<GatewayLogVO> pages = gatewayLogService.selectGatewayLogPage(Condition.getPage(query), gatewayLog);
		return R.data(pages);
	}
	/**
	 * @Description 查询接口访问记录
	 * @Author daina
	 * @Date 15:41 2020/3/23
	 * @param gatewayLog
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.log.vo.GatewayLogVO>>
	 **/
	@ApiLog("查询接口访问记录")
	@GetMapping("/listGatewayLogPage")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入gatewayLog")
	@Log(title = "查询接口访问记录", businessType = BusinessType.SELECT)
	public R<IPage<GatewayLogVO>> listGatewayLogPage(GatewayLogVO gatewayLog, Query query) {
		IPage<GatewayLogVO> pages = gatewayLogService.listGatewayLogPage(Condition.getPage(query), gatewayLog);
		return R.data(pages);
	}
	/**
	 * @Description 新增 网关访问记录表
	 * @Author xiafayu
	 * @Date 2020/2/24 14:56
	 * @param gatewayLog
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("网关访问记录新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入gatewayLog")
	@Log(title = "网关访问记录新增", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody GatewayLog gatewayLog) {
		return R.status(gatewayLogService.save(gatewayLog));
	}

	/**
	 * @Description 修改 网关访问记录表
	 * @Author xiafayu
	 * @Date 2020/2/24 14:56
	 * @param gatewayLog
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("网关访问记录修改")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入gatewayLog")
	@Log(title = "网关访问记录修改", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody GatewayLog gatewayLog) {
		return R.status(gatewayLogService.updateById(gatewayLog));
	}

	/**
	 * @Description 新增或修改 网关访问记录表
	 * @Author xiafayu
	 * @Date 2020/2/24 14:56
	 * @param gatewayLog
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("网关访问记录新增修改")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入gatewayLog")
	@Log(title = "网关访问记录新增修改", businessType = BusinessType.UPDATE)
	public R submit(@Valid @RequestBody GatewayLog gatewayLog) {
		return R.status(gatewayLogService.saveOrUpdate(gatewayLog));
	}

	
	/**
	 * @Description 删除 网关访问记录表
	 * @Author xiafayu
	 * @Date 2020/2/24 14:56
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("网关访问记录删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@Log(title = "网关访问记录删除", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(gatewayLogService.deleteLogic(Func.toLongList(ids)));
	}
	/**
	 * @Description 详情
	 * @Author xiafayu
	 * @Date 2020/2/24 14:54
	 * @param gatewayLog
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.log.entity.GatewayLog>
	 **/
	@ApiLog("网关访问记录详情")
	@GetMapping("/detailLog")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入gatewayLog")
	@Log(title = "网关访问记录详情", businessType = BusinessType.SELECT)
	public R<GatewayLogVO> detailLog(GatewayLogVO gatewayLog , Query query) {
		IPage<GatewayLogVO>  pages = gatewayLogService.selectGatewayLogVOPage(Condition.getPage(query), gatewayLog);
		GatewayLogVO detail = pages.getRecords().get(0);
		return R.data(detail);
	}
	/**
	 * @Description 分页 流水列表接口
	 * @Author wjh
	 * @Date 2020年12月7日14:33:52
	 * @param gatewayLog
	 * @param query
	 **/
	@ApiLog("流水列表")
	@GetMapping("/LogList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入gatewayLog")
	@Log(title = "流水列表接口", businessType = BusinessType.SELECT)
	public  R  LogList(GatewayLogVO gatewayLog , Query query) {
		if (gatewayLog.getIsFrom()!=1){
			if (gatewayLog.getOpcApiCode()!=null){
				//条件查询
				gatewayLog.setApiCodes(null);
			}else  if (gatewayLog.getApiId()!=null){
				// api--->得到apiCode
				List<GatewayLogVO> list=gatewayLogService.selectApiByApiId(gatewayLog);
				getApiCodes(gatewayLog, list);
			}else if (gatewayLog.getBaseProductId()!=null){
				// 产品----->api--->得到apiCode
				List<GatewayLogVO> list=gatewayLogService.selectApiByBaseProductId(gatewayLog);
				getApiCodes(gatewayLog, list);
			}else if (gatewayLog.getProductId()!=null){
				// 应用产品----->api--->得到apiCode
				List<GatewayLogVO> list=gatewayLogService.selectApiByProductId(gatewayLog);
				getApiCodes(gatewayLog, list);
			}else if (gatewayLog.getSysId()!=null){
				//系统--->产品----->api--->得到apiCode
				List<GatewayLogVO> list=gatewayLogService.selectApiCodeBySysId(gatewayLog);
				getApiCodes(gatewayLog, list);
			}
			if (gatewayLog.getOpcApiCode()==null && gatewayLog.getApiCodes()==null){
				return R.data(null);
			}
		}

		IPage<GatewayLogVO>  pages = gatewayLogService.selectGatewayLogVOPage(Condition.getPage(query), gatewayLog);
		return R.data(pages);
	}

	private void getApiCodes(GatewayLogVO gatewayLog, List<GatewayLogVO> list) {
		List<String> apiCodes=new ArrayList();

		if(list.size()==1&&list.get(0)==null){
			gatewayLog.setApiCodes(null); //没有范围内的apiCode
		}else {
			for (GatewayLogVO logVO:list){
				if (logVO!=null && null!=logVO.getApiCode()){
					apiCodes.add(logVO.getApiCode());
				}
			}
			List<String>  apiCodeList =new ArrayList(new TreeSet(apiCodes));
			String[] apiCodeArr= new String[apiCodeList.size()];
			for ( int i=0;i<apiCodeList.size();i++){
				String apiCode = apiCodeList.get(i);
				apiCodeArr[i]=apiCode;
			}
			gatewayLog.setApiCodes(apiCodeArr);
		}

	}
}
